cmake_minimum_required(VERSION 3.22)
project(selahengine)

# set proxy if needed
set(ENV{https_proxy} "http://127.0.0.1:10809")
# set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY  $<1:${CMAKE_SOURCE_DIR}>)
# set(CMAKE_LIBRARY_OUTPUT_DIRECTORY  $<1:${CMAKE_SOURCE_DIR}>)
# set(CMAKE_RUNTIME_OUTPUT_DIRECTORY  $<1:${CMAKE_SOURCE_DIR}>)

set(SELAH_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(SELAH_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

set(SELAH_OUTPUT_DIRECTORY ${SELAH_RUNTIME_OUTPUT_DIRECTORY})


set(SELAH_SHADER_OUTPUT_DIRECTORY ${SELAH_OUTPUT_DIRECTORY}/shaders)
set(SELAH_PLUGIN_OUTPUT_DIRECTORY ${SELAH_OUTPUT_DIRECTORY}/plugins)


set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/bin)
set(3RDPARTY_ROOT_DIRECTORY ${PROJECT_SOURCE_DIR}/External)
set(CPU_ARCH x64)


set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# MSVC C2102 : l-value
if (MSVC)
    add_compile_options(/permissive)
endif()

set_property(GLOBAL PROPERTY USE_FOLDERS ON)

function(CheckAndDownloadPackage NAME VERSION LOCAL_PATH URL)
    # Do we already have the correct version?
    if(NOT EXISTS ${LOCAL_PATH}/${VERSION}.ver)
        # Was there a previous version that we need to delete?
        if(EXISTS ${LOCAL_PATH})
            message(STATUS "Deleting old " ${NAME})
            file(REMOVE_RECURSE ${LOCAL_PATH})
        endif()
        message(STATUS "Obtaining " ${NAME} " " ${VERSION})
        file(DOWNLOAD ${URL} ${LOCAL_PATH}.zip)
        message(STATUS "Extracting " ${NAME})
        file(ARCHIVE_EXTRACT INPUT ${LOCAL_PATH}.zip DESTINATION ${LOCAL_PATH})
        file(REMOVE ${LOCAL_PATH}.zip)
        # Create an empty file so we know which version we have
        file(WRITE ${LOCAL_PATH}/${VERSION}.ver)
    endif()
endfunction()

if(WIN32)
    CheckAndDownloadPackage("Agility SDK" "v1.614.1" ${3RDPARTY_ROOT_DIRECTORY}/agilitysdk https://www.nuget.org/api/v2/package/Microsoft.Direct3D.D3D12/1.614.1)

    CheckAndDownloadPackage("DXC" "v1.8.2407" ${3RDPARTY_ROOT_DIRECTORY}/dxc https://github.com/microsoft/DirectXShaderCompiler/releases/download/v1.8.2407/dxc_2024_07_31.zip)
    # https://bellard.org/quickjs/binary_releases/quickjs-win-x86_64-2025-04-26.zip

    if(NOT EXISTS ${3RDPARTY_ROOT_DIRECTORY}/eigen-3.4.0)
        message(STATUS "Extracting Eigen")
        file(ARCHIVE_EXTRACT INPUT ${3RDPARTY_ROOT_DIRECTORY}/eigen-3.4.0.zip DESTINATION ${3RDPARTY_ROOT_DIRECTORY}/)
    endif()

endif()

# configure dxc
set(DXC_INCLUDE_PATH ${3RDPARTY_ROOT_DIRECTORY}/dxc/inc)
set(DXC_BIN_PATH ${3RDPARTY_ROOT_DIRECTORY}/dxc/bin/${CPU_ARCH})
set(DXC_LIB_PATH ${3RDPARTY_ROOT_DIRECTORY}/dxc/lib/${CPU_ARCH})
set(DXC_EXEC ${DXC_BIN_PATH}/dxc.exe)
set(DXC_BINARY ${DXC_BIN_PATH}/dxcompiler.dll)
set(DXIL_BINARY ${DXC_BIN_PATH}/dxil.dll)

# set(CMAKE_SUPPRESS_REGENERATION true)
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
set(CMAKE_SKIP_INSTALL_RULES True)

if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
    add_compile_options(-Wno-address-of-temporary)
endif()

add_subdirectory(External)

add_subdirectory(Engine)

add_subdirectory(Tests)

# file(GLOB_RECURSE SOURCE_FILES
#     *.h
#     *.c
#     *.cpp
#     *.hlsl
# )

# file(GLOB_RECURSE SHADER_FILES
#     *.hlsl
# )

# list(FILTER SOURCE_FILES EXCLUDE REGEX "^${CMAKE_CURRENT_SOURCE_DIR}/CMakeFiles")

# target_include_directories(selahengine PRIVATE ./)
# target_include_directories(selahengine PRIVATE ./External/imgui/)

# /*$(if:DX12.AgilitySDKRequired:true)*/target_include_directories(selahengine PRIVATE ./AgilitySDK/include/)/*$(endif)*/
# target_include_directories(selahengine PRIVATE ./DX12Utils/tinyexr/)
# target_include_directories(selahengine PRIVATE ./DX12Utils/tinyexr/deps/miniz/)

# target_link_directories(selahengine PRIVATE ./WinPixEventRuntime.1.0.230302001/bin/x64/)

# target_link_libraries(selahengine d3d12.lib dxgi.lib d3dcompiler.lib)

# target_sources(selahengine PUBLIC ${SOURCE_FILES})

# source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${SOURCE_FILES})

# set_source_files_properties(${SHADER_FILES} PROPERTIES HEADER_FILE_ONLY TRUE)

# set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT selahengine)
